<?php
namespace Admin\Controller;
use Think\Controller;

class RoleeditController extends CommonController {
    public function index(){
        
    	$id = I('get.id');

    	$role = M('auth_group')->find($id);

    	$this->assign('role',$role);

    	$rules = explode(',',$role['rules']);

    	$cate = M('auth_category')->select();

    	$rule = M('auth_rule')->select();

    	foreach ($cate as $k => $v) {
    		foreach ($rule as $key => $value) {
    			if(in_array($value['id'], $rules)){
    				$rule[$key]['myrule']='1';
    			}
    			if($v['cid']==$value['cid']){
    				$cate[$k]['content'][]=$rule[$key];
    			}
    		}
    	}
/*
    	echo '<pre>';
    	print_r($cate);
    	echo '</pre>';*/

    	$this->assign('cate',$cate);

        $this->display();
    }

    public function edit(){
    	$data = I('post.');

    	$name = $data['name'];

    	/*var_dump($data);exit;*/

    	$data['rules']=rtrim($data['rules'],',');

    	$result=M('auth_group')->where(['title'=>$name])->save($data);

    	M('ruletogroup')->where(['gname'=>$name])->delete();

    	for($i=0;$i<count($data['rule']);$i++){
    		$role[]=array('gname'=>$data['name'],'rid'=>$data['rule'][$i]);
    	}

    	M('ruletogroup')->addAll($role);

    	if($result){
    		$this->ajaxReturn(['error'=>0,'info'=>'修改成功']);
    	}else{
    		$this->ajaxReturn(['error'=>1,'info'=>'修改失败']);
    	}
    }
}

?>